# Copyright (c) 2025, yuxinyong and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document


class SamplingJobCard(Document):
    def on_submit(self):
        self.update_sample()
    
    def on_cancel(self):
        self.update_sample()
        
    def update_sample(self):
        if not self.sample:
            return
        
        sample_doc = frappe.get_doc("Shoe Sample", self.sample)
        if self.sample_process_row_id:
            for data in sample_doc.processes:
                if data.get("name") == self.sample_process_row_id:
                    data.completed_qty = self.completed_qty
                    #data.process_loss_qty = process_loss_qty
                    data.actual_process_time = self.total_time_in_mins
                    data.actual_start_time = self.actual_start_time
                    data.actual_end_time = self.actual_end_time

            sample_doc.flags.ignore_validate_update_after_submit = True
            sample_doc.save()